﻿using Project.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.DAL
{
    public class CartDetailDal : BaseDal<milktea_cart_detail>
    {
        Entities model = new Entities();

        public void CartJiaGoods(milktea_cart_detail detail) { 
            var data = model.milktea_cart.Where(c => c.cart_id == detail.Cart_id).FirstOrDefault();

            data.quantity = data.quantity+ detail.quantity;
            data.zong_price = data.zong_price+ detail.price;

            model.SaveChanges();

        }

        public bool CartDetailAdd(milktea_cart_detail detail) {
            var list = model.milktea_cart_detail.Where(d => d.Cart_id == detail.Cart_id);

            var data = list.Where(d => d.Goods_Id == detail.Goods_Id && string.Equals(d.specification,detail.specification)).FirstOrDefault();

            if (data != null)
            {
                data.quantity += detail.quantity;
                data.price += detail.price;

                int count =  model.SaveChanges();

                return count > 0;

            }
            else { 
                model.milktea_cart_detail.Add(detail);
                int count = model.SaveChanges();

                return count > 0;
            }

        }

        public void JiaCart(int cid,int shu, decimal price) {
            var data = model.milktea_cart.Where(c => c.cart_id == cid).FirstOrDefault();

            data.quantity += shu;
            data.zong_price += price * shu;

            model.SaveChanges();

        }

        public void JianCart(int cid, int shu, decimal price)
        {
            var data = model.milktea_cart.Where(c => c.cart_id == cid).FirstOrDefault();

            data.quantity -= shu;
            data.zong_price -= price * shu;

            model.SaveChanges();
        }
    }
}
